<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2种方法计算居中</title>
    <style>
        *{
            margin: 0;
            padding:0;
        }
        .box{
            height: 100px;
            width: 300px;
            border:solid 2px;
        }
        /* 设置margin水平居中 */
        .box2{
            width: 50px;
            height: 94px;
            margin:0 auto;
            border:solid 1px;
            background-color: red           
        }
        .box3{
            height: 100px;
            width: 300px;
            margin-top: 10px;
            border:solid 2px;
        }
        /* 计算父盒子宽度的一半，减去子盒子的宽度， */
        .box4{
            width: 50px;
            height: 50px;
            margin-left: 125px;
            border:solid 1px;
            background-color:yellow;     
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box2"></div>
    </div>
    <div class="box3">
        <div class="box4"></div>
    </div>
</body>
</html>